package edu.uta.tsrh.db.commandpattern;

import com.mysql.jdbc.Connection;
import edu.uta.tsrh.db.DAO;
import edu.uta.tsrh.db.DBQueryExecuter;
import edu.uta.tsrh.model.HashMD5;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Referenced classes of package edu.uta.tsrh.db.commandpattern:
//            MySQLCmd

public class ChangePassword
    implements MySQLCmd
{

    private String CurrentPassword;
    private String NewPassword;
    private String username;

    public ChangePassword(String CurrentPassword, String NewPassword,String username)
    {
        
        this.CurrentPassword = CurrentPassword;
        this.NewPassword = NewPassword;
        this.username = username;
    }

    public String execute()
    {
        System.out.println("1");
        Pattern p = Pattern.compile("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%?*^&_!~]).{8,20})");
        Matcher m = p.matcher(NewPassword);
        boolean b = m.matches();
        System.out.println("2");
        if(!b)
        {
            int f = 0;
            return Integer.toString(f);
        } else
        {
            this.CurrentPassword = HashMD5.hashpw(this.CurrentPassword);
            this.NewPassword = HashMD5.hashpw(this.NewPassword );
            String sqlQuery = buildQuery();
            System.out.println((new StringBuilder()).append("query is ").append(sqlQuery).toString());
            Connection connection = DAO.getConnection();
            DBQueryExecuter queryExecuter = new DBQueryExecuter();
            int result = queryExecuter.modify(sqlQuery, connection);
            DAO.closeConnection();
            return Integer.toString(result);
        }
    }

    private String buildQuery()
    {
        return (new StringBuilder()).append("Update user set password ='").append(NewPassword).append("'where username ='").append(username).append("'and password = '").append(CurrentPassword).append("'").toString();
    }

    public boolean isReversible()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void undo()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   
}
